package cn.ujn.medicalbackend.controller;

import cn.ujn.medicalbackend.common.BaseResponse;
import cn.ujn.medicalbackend.common.ErrorCode;
import cn.ujn.medicalbackend.common.ResultUtils;
import cn.ujn.medicalbackend.domain.Doctor;
import cn.ujn.medicalbackend.domain.DoctorLevel;
import cn.ujn.medicalbackend.domain.TreatType;
import cn.ujn.medicalbackend.dto.DoctorQuery;
import cn.ujn.medicalbackend.exception.BusinessException;
import cn.ujn.medicalbackend.request.DoctorAddRequest;
import cn.ujn.medicalbackend.request.DoctorDeleteRequest;
import cn.ujn.medicalbackend.request.DoctorUpdateRequest;
import cn.ujn.medicalbackend.service.AccountService;
import cn.ujn.medicalbackend.service.DoctorService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author XinCheng
 * date 2024-10-24
 */
@RestController
@RequestMapping("/doctor")
@CrossOrigin(origins = {"http://localhost:9092"})
public class DoctorController {

    @Autowired
    private AccountService accountService;

    @Autowired
    private DoctorService doctorService;


    @GetMapping("/list/page")
    public BaseResponse<Page<Doctor>> getDoctorListByPage(DoctorQuery doctorQuery) {
        if (doctorQuery == null) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        Doctor doctor = new Doctor();
        BeanUtils.copyProperties(doctorQuery, doctor);
        QueryWrapper<Doctor> queryWrapper = new QueryWrapper<>(doctor);
        Page<Doctor> page = new Page<>(doctorQuery.getPageNum(), doctorQuery.getPageSize());
        Page<Doctor> tempPage = doctorService.page(page, queryWrapper);
        Page<Doctor> resultPage = doctorService.DoctorPageTransfer(tempPage);
        return ResultUtils.success(resultPage);
    }

    @GetMapping("/list")
    public BaseResponse<List<Doctor>> getDoctorList(DoctorQuery doctorQuery) {
        if (doctorQuery == null) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        Doctor doctor = new Doctor();
        BeanUtils.copyProperties(doctorQuery, doctor);
        QueryWrapper<Doctor> queryWrapper = new QueryWrapper<>(doctor);
        List<Doctor> doctorList = doctorService.list(queryWrapper);
        List<Doctor> doctors=doctorService.doctorListTransfer(doctorList);

        return ResultUtils.success(doctors);
    }

    @PostMapping("/add")
    public BaseResponse<Long> addDoctor(@RequestBody DoctorAddRequest doctorAddRequest, HttpServletRequest request) {
        if (doctorAddRequest == null) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        long doctorId = doctorService.addDoctor(doctorAddRequest, request);
        return ResultUtils.success(doctorId);
    }

    @PostMapping("/update")
    private BaseResponse<Boolean> updateDoctor(@RequestBody DoctorUpdateRequest doctorUpdateRequest, HttpServletRequest request) {

        if (doctorUpdateRequest == null) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        boolean result = doctorService.updateDoctor(doctorUpdateRequest,request);
        return ResultUtils.success(result);
    }

    @PostMapping("/delete")
    private BaseResponse<Boolean> deleteDoctor(@RequestBody DoctorDeleteRequest doctorDeleteRequest, HttpServletRequest request) {
        if (doctorDeleteRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = doctorService.deleteDoctor(doctorDeleteRequest, request);
        return ResultUtils.success(result);
    }
}
